package com.imdroid.lite.activities;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.imdroid.lite.IMDroid;
import com.imdroid.lite.R;
import com.imdroid.lite.model.Destination;
import com.imdroid.lite.process.ProcessFactory;
import com.imdroid.lite.process.UserProcess;

public class NavigatorPointSetting extends BaseActivity {
	/**
	 * The TAG of the activity
	 */
	private String TAG = "NavigatorPointSetting";
	/**
	 * The hash map to indicate the controls
	 */
	private Map<View, String> addressButtonControls = new HashMap<View, String>();
	/**
	 * The hash map to indicate the controls
	 */
	private Map<View, String> addressTitleControls = new HashMap<View, String>();
	/**
	 * The hash map to indicate the controls
	 */
	private Map<View, String> addressTitleNames = new HashMap<View, String>();
	/**
	 * The name of the address
	 */
	private String addressNames[] = { "home", "company", "common1", "common2",
			"common3" };
	/**
	 * The name of the address
	 */
	private String addressTitles[] = {
			IMDroid.getAppContext().getString(R.string.loc_btn_home),
			IMDroid.getAppContext().getString(R.string.loc_btn_company),
			IMDroid.getAppContext().getString(R.string.loc_btn_common1),
			IMDroid.getAppContext().getString(R.string.loc_btn_common2),
			IMDroid.getAppContext().getString(R.string.loc_btn_common3) };
	/**
	 * The text view of the address
	 */
	private TextView tv_home, tv_company, tv_common1, tv_common2, tv_common3;
	/**
	 * The button of sending the address
	 */
	private Button btn_home, btn_company, btn_common1, btn_common2,
			btn_common3;
	/**
	 * The destination
	 */
	private Destination destination;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loc_navigator_points);
		// 初始化各控件
		tv_home = (TextView) findViewById(R.id.loc_tv_map_set_home);
		tv_company = (TextView) findViewById(R.id.loc_tv_map_set_company);
		tv_common1 = (TextView) findViewById(R.id.loc_tv_map_set_common1);
		tv_common2 = (TextView) findViewById(R.id.loc_tv_map_set_common2);
		tv_common3 = (TextView) findViewById(R.id.loc_tv_map_set_common3);
		btn_home = (Button) findViewById(R.id.loc_btn_map_set_home);
		btn_company = (Button) findViewById(R.id.loc_btn_map_set_company);
		btn_common1 = (Button) findViewById(R.id.loc_btn_map_set_common1);
		btn_common2 = (Button) findViewById(R.id.loc_btn_map_set_common2);
		btn_common3 = (Button) findViewById(R.id.loc_btn_map_set_common3);

		tv_home.setOnClickListener(listener);
		tv_company.setOnClickListener(listener);
		tv_common1.setOnClickListener(listener);
		tv_common2.setOnClickListener(listener);
		tv_common3.setOnClickListener(listener);

		// 初始化控件-名称的hash map
		addressButtonControls.put(btn_home, addressNames[0]);
		addressButtonControls.put(btn_company, addressNames[1]);
		addressButtonControls.put(btn_common1, addressNames[2]);
		addressButtonControls.put(btn_common2, addressNames[3]);
		addressButtonControls.put(btn_common3, addressNames[4]);
		// 初始化控件-名称的hash map
		addressTitleControls.put(tv_home, addressNames[0]);
		addressTitleControls.put(tv_company, addressNames[1]);
		addressTitleControls.put(tv_common1, addressNames[2]);
		addressTitleControls.put(tv_common2, addressNames[3]);
		addressTitleControls.put(tv_common3, addressNames[4]);

		// 初始化控件-名称的hash map
		addressTitleNames.put(tv_home, addressTitles[0]);
		addressTitleNames.put(tv_company, addressTitles[1]);
		addressTitleNames.put(tv_common1, addressTitles[2]);
		addressTitleNames.put(tv_common2, addressTitles[3]);
		addressTitleNames.put(tv_common3, addressTitles[4]);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	protected void onResume() {
		Log.d(TAG, "onResume");
		super.onResume();
		// 根据是否设置目的地，更改控件的颜色、状态
		feedSavedAddress();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onPause");
		super.onPause();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onStop");
		super.onStop();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		Log.d(TAG, "onDestroy...");
		super.onDestroy();
	}

	/**
	 * The action when clicking the back button
	 * 
	 * @param view
	 *            the button of back
	 */
	public void back(View view) {
		// startActivity(new Intent(this, MainActivity.class));
		finish();
	}

	/**
	 * The action when clicking the skip button
	 * 
	 * @param view
	 *            the button of back
	 */
	public void skip(View view) {
		startActivity(new Intent(this, NavigatorPointsPickup.class));
	}

	/**
	 * The onclick of the the text view
	 */
	private android.view.View.OnClickListener listener = new android.view.View.OnClickListener() {
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.view.View.OnClickListener#onClick(android.view.View)
		 */
		public void onClick(View v) {
			sendOrLocate(v);
		}
	};

	/**
	 * Feed the saved address to the controls
	 */
	private void feedSavedAddress() {
		// 设置常用地址栏的颜色
		for (View t : addressTitleControls.keySet()) {
			String addressName = addressTitleControls.get(t);
			TextView textView = (TextView) t;
			Destination destination = Destination.getDestination(addressName);
			Log.d(TAG, "addressName : " + addressName + "--" + destination);

			Button btn = null;
			for (Entry<View, String> ent : addressButtonControls.entrySet()) {
				if (addressName.equals(ent.getValue())) {
					btn = (Button) (ent.getKey());
					break;
				}
			}

			// 如果没有设置常用地址，则设置为灰色
			if (destination == null) {
				textView.setBackgroundResource(R.drawable.navi_point_disabled_selector);

				btn.setText(getResources().getString(R.string.loc_setting));
				textView.setText(addressTitleNames.get(textView));
			} else {
				textView.setBackgroundResource(R.drawable.navi_point_active_selector);
				if (addressName.equals("home"))
					textView.setText(this.getString(R.string.loc_btn_home)
							+ "-" + destination.getAddress());
				else if (addressName.equals("company"))
					textView.setText(this.getString(R.string.loc_btn_company)
							+ "-" + destination.getAddress());
				else
					textView.setText(destination.getAddress());
				btn.setText(getResources().getString(
						R.string.loc_setting_delete));
			}

		}
		// 设置常用按钮的文字
		for (View view : addressButtonControls.keySet()) {
			String addressName = addressButtonControls.get(view);
			Button button = (Button) view;
			Destination destination = Destination.getDestination(addressName);
			// 如果已经有目的地，则修改按钮的文字
			if (destination != null)
				button.setBackgroundResource(R.drawable.navi_point_active_selector);
			else
				button.setBackgroundResource(R.drawable.navi_point_disabled_selector);
		}
	}

	/**
	 * Send or locate
	 * 
	 * @param view
	 *            thew view
	 */
	public void remove(View view) {
		Button button = (Button) view;
		String addressName = addressButtonControls.get(button);
		if (addressName != null && addressName.length() > 0) {
			Destination d = Destination.getDestination(addressName);
			if (d != null) {
				final String name = addressButtonControls.get(view);
				AlertDialog.Builder buidler = new AlertDialog.Builder(this);
				buidler.setMessage(R.string.loc_confirm_remove);

				buidler.setPositiveButton(R.string.loc_dia_y,
						new OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								// 移除常用地址
								Destination.clearDestination(name);
								// 更新按钮状态
								feedSavedAddress();
							}
						});
				buidler.setNegativeButton(R.string.loc_dia_n, null);
				buidler.show();
			} else {
				sendOrLocate(addressName);
			}
		}
	}

	/**
	 * Send the address
	 * 
	 * @param view
	 *            the view of button
	 */
	public void sendOrLocate(View view) {
		String addressName = addressTitleControls.get(view);
		Log.d(TAG, "----" + addressName);
		if (addressName != null && addressName.length() > 0) {
			Destination destination = Destination.getDestination(addressName);
			if (destination == null) {
				locate(addressName);
			} else {
				this.destination = destination;
				// 用户登录
				new InProgressBarTask(this, R.string.loc_wait_title,
						R.string.loc_wait_text).execute(new Object());
			}
		}
	}

	public void sendOrLocate(String addressName) {
		Log.d(TAG, "----" + addressName);
		if (addressName != null && addressName.length() > 0) {
			Destination destination = Destination.getDestination(addressName);
			if (destination == null) {
				locate(addressName);
			} else {
				this.destination = destination;
				// 用户登录
				new InProgressBarTask(this, R.string.loc_wait_title,
						R.string.loc_wait_text).execute(new Object());
			}
		}
	}

	/**
	 * locate the address
	 * 
	 * @param addressName
	 *            the address name
	 */
	public void locate(String addressName) {
		Intent intet = new Intent(this, NavigatorPointsPickup.class);
		intet.putExtra("addressName", addressName);
		this.startActivityForResult(intet, 1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.imdroid.lite.activities.BaseActivity#actionInProgressBar(java.lang
	 * .Object[])
	 */
	protected void actionInProgressBar(Object... params) throws Exception {
		UserProcess process = (UserProcess) ProcessFactory
				.getProcess(UserProcess.class);
		process.sendDestination(destination, NavigatorPointSetting.this);

		messageHandler.post(new Runnable() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see java.lang.Runnable#run()
			 */
			public void run() {
				// 提示成功
				final Activity activity = NavigatorPointSetting.this;
				AlertDialog.Builder buidler = new AlertDialog.Builder(activity);
				buidler.setMessage(activity
						.getString(R.string.loc_send_success));
				buidler.setPositiveButton("Ok", new OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						// 返回主界面
						activity.finish();
					}
				});
				buidler.show();

			}
		});
	}
}
